﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.orderLinqDAL;
using dhSystemBLL.orderData;
using baseSet.importExport;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess;
using dhSystemBLL.basicData;
using baseSet;
using dhDataAcess.baseBLL;
using dhsystem.produceForm;
using dhsystem.produceForm.picking;
using dhSystemBLL.produceBLL;

namespace dhsystem.orderForm
{
    public partial class FrmscrlMasterAdd : Form
    {
        orderDataSet.scrlDetailDataTable pbomDetailTable = new orderDataSet.scrlDetailDataTable();
        scrlDetailModel scrlDetailRow = new scrlDetailModel();
        scrlBLL scr = new scrlBLL();
        deptBLL dept = new deptBLL();
        productblBLL productbl = new productblBLL();

        workerListBLL workerList = new workerListBLL();
        scrlMasterModel scrlMasterRow = new scrlMasterModel();
        BindingSource b2;
        string state;
        public FrmscrlMasterAdd(BindingSource b1, string _state)
        {
            InitializeComponent();
            state = _state;
            b2 = b1;
        }
        //打开窗口时绑定数据
        private void FrmscrlMasterAdd_Load(object sender, EventArgs e)
        {
            textbillNo.Focus();
            bindingSourcedeptList.DataSource = dept.getdeptListAll();
            comboBoxdeptNum.DataSource = bindingSourcedeptList;
            comboBoxdeptNum.DisplayMember = "deptName";
            comboBoxdeptNum.ValueMember = "deptNum";
            if (state == "新增")
            {
                textbillNo.Text = "L" + GlobalInfo.Date + "-" + basicSql.getOrderSerail("领料单序列号");
                bindingSourcescrlDetail .DataSource = scr .getscrlDetailAllBybillNo  (textbillNo .Text );
                dGridDetail.DataSource = bindingSourcescrlDetail;

            }
            bindingSourceworkerList.DataSource = workerList.getworkerListAll();
            comboBoxworkername.DataSource = bindingSourceworkerList;
            comboBoxworkername.DisplayMember = "name";
            comboBoxworkername.ValueMember = "workerNum";

            if (state == "编辑")
            {
                comboBoxdeptNum.DataBindings.Add("text", b2, "deptNum");
                comboBoxworkername.DataBindings.Add("SelectedValue", b2, "workername");
                textbillNo.DataBindings.Add("text", b2, "billNo");
                dateTimebillDate.DataBindings.Add("value", b2, "billDate");
                textorderDetailNo.DataBindings.Add("text", b2, "orderDetailNo");
               
                textmark.DataBindings.Add("text", b2, "mark");
                bindingSourcescrlDetail .DataSource  = scr .getscrlDetailAllBybillNo (textbillNo .Text );
                dGridDetail.DataSource = bindingSourcescrlDetail;
            }
            DcolumnSet.colSet(moduleInfo.scrlDetail , dGridDetail);
            dGridDetail.AllowUserToAddRows = false;
           
        }
        int i = 0;
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (textbillNo .Text  == "")
            {
                MessageBox.Show("请输入领料单号");
            }
            else
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否保存领料表信息！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {

                        if (i == 0 && state == "新增")
                        {
                            state = "新增";
                        }
                        else
                        {
                            state = "编辑";
                        }
                        i++;
                        dhDataAcess.dataAcess.orderDataSet.scrlDetailDataTable ptable = (dhDataAcess.dataAcess.orderDataSet.scrlDetailDataTable)bindingSourcescrlDetail.DataSource;
                        scrlMasterRow.billNo = textbillNo.Text;
                        scrlMasterRow.deptNum = comboBoxdeptNum.SelectedValue.ToString();
                        scrlMasterRow.workername = comboBoxworkername.SelectedValue.ToString();
                        scrlMasterRow.billDate = dateTimebillDate.Value;
                        scrlMasterRow.orderDetailNo = textorderDetailNo.Text;
                        scrlMasterRow.billStatus = "新建";
                        scrlMasterRow.createDate = DateTime.Now;
                        scrlMasterRow.createUser = GlobalInfo.UserName;
                        scrlMasterRow.updateDate = DateTime.Now;
                        scrlMasterRow.updateUser = GlobalInfo.UserName;
                        scrlMasterRow.mark = textmark.Text;
                        scr .scrlInsert (scrlMasterRow , ptable, state,textbillNo .Text ,GlobalInfo .Date  );
                        ptable.AcceptChanges();
                        bindingSourcescrlMaster.DataSource = scr.getscrlMasterAll();
                        MessageBox.Show("保存成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //添加子件
        private void buttAdd_Click(object sender, EventArgs e)
        {
            FrmscrlDetailAdd  scrlDetail = new FrmscrlDetailAdd (bindingSourcescrlDetail , "新增", textbillNo .Text );
            scrlDetail.ShowDialog();
        }
        //编辑子件
        private void buttedit_Click(object sender, EventArgs e)
        {
            FrmscrlDetailAdd scrlDetail = new FrmscrlDetailAdd(bindingSourcescrlDetail, "编辑", textbillNo.Text);
            scrlDetail.ShowDialog();
        }
        //删除子件
        private void buttDel_Click(object sender, EventArgs e)
        {

            try
            {
                string id = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells[0].Value.ToString();
                DialogResult result = MessageBox.Show("是否删除：" + id + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    dGridDetail.Rows.Remove(dGridDetail.CurrentRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //菜单栏退出按钮
        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //菜单栏保存按钮
        private void toolSave_Click(object sender, EventArgs e)
        {
            if (textbillNo.Text == "")
            {
                MessageBox.Show("请输入领料单号");
            }
            else
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否保存领料表信息！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {

                        if (i == 0 && state == "新增")
                        {
                            state = "新增";
                        }
                        else
                        {
                            state = "编辑";
                        }
                        i++;
                        dhDataAcess.dataAcess.orderDataSet.scrlDetailDataTable ptable = (dhDataAcess.dataAcess.orderDataSet.scrlDetailDataTable)bindingSourcescrlDetail.DataSource;
                        scrlMasterRow.billNo = textbillNo.Text;
                        scrlMasterRow.deptNum = comboBoxdeptNum.SelectedValue.ToString();
                        scrlMasterRow.workername = comboBoxworkername.SelectedValue.ToString();
                        scrlMasterRow.billDate = dateTimebillDate.Value;
                        scrlMasterRow.orderDetailNo = textorderDetailNo.Text;
                        scrlMasterRow.billStatus = "新建";
                        scrlMasterRow.createDate = DateTime.Now;
                        scrlMasterRow.createUser = GlobalInfo.UserName;
                        scrlMasterRow.updateDate = DateTime.Now;
                        scrlMasterRow.updateUser = GlobalInfo.UserName;
                        scrlMasterRow.mark = textmark.Text;
                        scr.scrlInsert(scrlMasterRow, ptable, state, textbillNo.Text, GlobalInfo.Date);
                        ptable.AcceptChanges();
                        bindingSourcescrlMaster.DataSource = scr.getscrlMasterAll();
                        MessageBox.Show("保存成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //菜单栏删除按钮
        private void toolDel_Click(object sender, EventArgs e)
        {
            try
            {
                string id = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells[0].Value.ToString();
                DialogResult result = MessageBox.Show("是否删除：" + id + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    dGridDetail.Rows.Remove(dGridDetail.CurrentRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //菜单栏新增按钮
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            state = "新增";
            textbillNo.Focus();
            textorderDetailNo.Text = "";
            textmark.Text = "";
            bindingSourcedeptList.DataSource = dept.getdeptListAll();
            comboBoxdeptNum.DataSource = bindingSourcedeptList;
            comboBoxdeptNum.DisplayMember = "deptName";
            comboBoxdeptNum.ValueMember = "deptNum";
      
                textbillNo.Text = "L" + GlobalInfo.Date + "-" + basicSql.getOrderSerail("领料单序列号");
                bindingSourcescrlDetail.DataSource = scr.getscrlDetailAllBybillNo(textbillNo.Text);
                dGridDetail.DataSource = bindingSourcescrlDetail;

            
            bindingSourceworkerList.DataSource = workerList.getworkerListAll();
            comboBoxworkername.DataSource = bindingSourceworkerList;
            comboBoxworkername.DisplayMember = "name";
            comboBoxworkername.ValueMember = "workerNum";

        
            DcolumnSet.colSet(moduleInfo.scrlDetail, dGridDetail);
            dGridDetail.AllowUserToAddRows = false;
        }
        //选择漂染单号
        private void buttworkerselect_Click(object sender, EventArgs e)
        {
            string field;
            field = "billNo";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.productblMaster  , field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                textorderDetailNo.Text  = productselect.pname;
            }
        }

        private void textorderDetailNo_TextChanged(object sender, EventArgs e)
        {
            purchase.productblDetailDataTable procutblTable = productbl.getsproductblDetailAllBybillNo(textorderDetailNo.Text);
            orderDataSet.scrlDetailDataTable scrlTable = new orderDataSet.scrlDetailDataTable();
            foreach (purchase.productblDetailRow  productblRow in procutblTable)
            {
                orderDataSet.scrlDetailRow  scrlRow = scrlTable.NewscrlDetailRow();
                scrlRow.matNum = productblRow.matNum;
                scrlRow.matName = basicSql.getmatNamebymaterialList(productblRow.matNum);
                scrlRow.matSpec = basicSql.getmatSpecbymaterialList(productblRow.matNum);
                scrlRow.unit = basicSql.getunitbymaterialList(productblRow.matNum);
                //if (!productblRow.IsmatNameNull())
                //{
                //    scrlRow.matName = productblRow.matName;
                //}

                //else { scrlRow.matName = ""; }
                if (!productblRow.IsstockqtyNull ())
                {
                    scrlRow.qty  = productblRow.stockKg ;
                }
                else { scrlRow.qty = 0; }
                //if (!productblRow.IspzNull())
                //{
                //    scrlRow.pz = productblRow.pz;
                //}
                //else { scrlRow.pz = ""; }
                scrlTable.AddscrlDetailRow(scrlRow);
            }
            bindingSourcescrlDetail.DataSource = scrlTable;
            dGridDetail.DataSource = bindingSourcescrlDetail;
        }
        //选择领料部门
        private void button2_Click(object sender, EventArgs e)
        {
            string field;
            field = "deptNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.deptList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxdeptNum.SelectedValue = productselect.pname;
            }
        }
        //选择领料人
        private void button1_Click(object sender, EventArgs e)
        {
            string field;
            field = "workerNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.workerList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxworkername.SelectedValue = productselect.pname;
            }
        }


    }
}
